package com.sky.controller.admin;

import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Setmeal;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.SetmealService;
import com.sky.vo.SetmealVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/admin/setmeal")
@Slf4j
@Api(tags = "管理端套餐接口")
public class SetmealAdminController {
    @Autowired
    private SetmealService setmealService;

    /**
     * 新增套餐
     *
     * @param setmealDTO
     * @return
     */
    @ApiOperation("新增套餐")
    @PostMapping
    @CacheEvict(cacheNames = "setmealCache", key = "#setmealDTO.categoryId")
    public Result addSetmeal(@RequestBody SetmealDTO setmealDTO) {
        log.info("新增套餐{}", setmealDTO);
        setmealService.addSet(setmealDTO);
        return Result.success();
    }


    /**
     * 套餐分页查询
     *
     * @param setmealPageQueryDTO
     * @return
     */
    @ApiOperation("套餐分页查询")
    @GetMapping("/page")
    public Result<PageResult> SetmealPageSearch(SetmealPageQueryDTO setmealPageQueryDTO) {
        log.info("套餐分页查询{}", setmealPageQueryDTO);
        Long count = setmealService.pageSearchCount(setmealPageQueryDTO);
        List<SetmealVO> setmealList = setmealService.pageSearchList(setmealPageQueryDTO);
        PageResult pageResult = new PageResult();
        pageResult.setTotal(count);
        pageResult.setRecords(setmealList);
        return Result.success(pageResult);
    }


    /**
     * 根据id查询套餐
     *
     * @param id
     * @return
     */
    @ApiOperation("根据id查询套餐")
    @GetMapping("/{id}")
    public Result<SetmealVO> getByid(@PathVariable Integer id) {
        log.info("id{}", id);
        SetmealVO setmealVO = new SetmealVO();
        setmealVO = setmealService.getByid(id);
        return Result.success(setmealVO);
    }


    /**
     * 套餐启售停售
     *
     * @param status
     * @param id
     * @return
     */
    @ApiOperation("套餐启售停售")
    @PostMapping("/status/{status}")
    @CacheEvict(cacheNames = "setmealCache", allEntries = true)
    public Result StatusChange(@PathVariable Integer status, Integer id) {
        log.info("套餐状态更改status{}id{}", status, id);
        setmealService.ChangeStatus(status, id);
        return Result.success();
    }

    /**
     * 套餐修改
     *
     * @param setmealDTO
     * @return
     */
    @ApiOperation("套餐修改")
    @PutMapping()
    @CacheEvict(cacheNames = "setmealCache", allEntries = true)
    public Result ChangeSet(@RequestBody SetmealDTO setmealDTO) {
        log.info("套餐修改{}", setmealDTO);
        setmealService.changeSet(setmealDTO);
        return Result.success();
    }

    @ApiOperation("批量删除套餐")
    @DeleteMapping()
    @CacheEvict(cacheNames = "setmealCache", allEntries = true)
    public Result DropSetmeal(Integer ids[]) {
        log.info("删除套餐{}", ids);
        setmealService.DropSetmeal(ids);
        return Result.success();
    }
}
